package com.topisv.tms.repository.work;

import java.util.List;
import java.util.Map;

import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import com.topisv.tms.entity.work.WorkTask;

/**
 * 作业任务_DAO
 * @author ch
 *
 */
public abstract interface WorkTaskDao extends PagingAndSortingRepository<WorkTask, Long>, JpaSpecificationExecutor<WorkTask>
{

	@Query("select t from WorkTask t where t.batchCode=? order by t.createTime")
	List<WorkTask> loadTaskByBatchCode(String batchCode);
	
	@Query("select t from WorkTask t where t.batchCode=? and t.status=? ")
	List<WorkTask> loadTask(String batchCode,String status);

	@Query("select t from WorkTask t where t.taskCode=?")
	WorkTask loadTaskByTaskCode(String taskCode);
	
	@Query("select new map(t.printStatus as printStatus, t.packformStatus as packformStatus, t.customerCode as customerCode, t.batchCode as batchCode) from WorkTask t where t.taskCode=?")
	Map<String, String> findTaskPrintStatusByTaskCode(String taskCode);
	
	@Query("select t from WorkTask t where t.taskCode=? and t.status=?")
	WorkTask loadTaskByTaskCodeAndStatus(String taskCode, String status);

	@Modifying
	@Query("update WorkTask set invoiceStatus=?1 where taskCode=?2")
	int updateInvoiceStatus(String invoiceStatus,String taskCode);
	
	/**
	 * 更新捡货的状态
	 * @param pickNum
	 * @param Status
	 * @param taskCode
	 * @return int
	 */
	@Modifying
	@Query("update WorkTask set pickedNum=?1,status=?2 where taskCode=?3")
	int updateTaskStatusForPick(Long pickNum,String Status,String taskCode);
	
	/**
	 * 更新打单的状态
	 * @param expressStatus
	 * @param printStatus
	 * @param taskCode
	 * @return int
	 */
	@Modifying
	@Query("update WorkTask set expressStatus=?1,printStatus=?2 where taskCode=?3")
	int updateExpressPrintStatus(String expressStatus,String printStatus,String taskCode);
	
	/**
	 * 更新打随箱单的状态
	 * @param packformStatus
	 * @param printStatus
	 * @param taskCode
	 * @return int
	 */
	@Modifying
	@Query("update WorkTask set packformStatus=?1,printStatus=?2 where taskCode=?3")
	int updatePackPrintStatus(String packformStatus,String printStatus,String taskCode);
	
	/**
	 * 更新作业任务的分拣筐
	 * @param boxCode
	 * @param status
	 * @param taskCode
	 * @return int
	 */
	@Modifying
	@Query("update WorkTask set boxCode=?1,status=?2 where taskCode=?3")
	int updateBoxCode(String boxCode,String status,String taskCode);
	
	/**
	 * 更新打印拣货单状态
	 * @param boxCode
	 * @param status
	 * @param taskCode
	 * @return int
	 */
	@Modifying
	@Query("update WorkTask set pickinfoStatus=?1 where taskCode=?2")
	int updatePickInfoStatus(String status,String taskCode);
	
	/**
	 * 修改拣货单打印次数
	 * @param taskCode 任务单号
	 * @param printFlag 打印次数
	 */
	@Modifying
	@Query("update WorkTask set printFlag=?2 where taskCode=?1")
	void updateTaskPrintFlag(String taskCode, int printFlag);
	
	
}